<template>
  <div class="alarm-table" v-loading="loading">
    <el-table :data="tableData" border style="width: 100%; flex: 1">
      <el-table-column prop="areaName" label="所在区域" width="150" align="center" />
      <el-table-column prop="alarmSourceName" label="报警来源" width="100" align="center" />
      <el-table-column prop="alarmTypeName" label="报警类型" align="center" width="110" />
      <el-table-column prop="beginTime" label="报警时间" width="110" align="center" />
      <el-table-column prop="content" label="报警原因" header-align="center" min-width="260" />
      <el-table-column prop="keepTimeStr" label="持续时长" width="100" align="center" />
      <el-table-column prop="keepTimeStr" label="操作" width="140" align="center">
        <template #default="{ row }">
          <el-button link type="primary" size="small" @click="handleRowClick(row)">
            明细
          </el-button>
          <el-button link type="success" size="small" @click="goBackTrack(row)">
            回溯
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="pagination"
      v-model:current-page="searchParams.pageNumber"
      :page-size="searchParams.pageSize"
      :total="searchParams.total"
      layout="total, prev, pager, next"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue'
import { getUnreadAlarmList } from '@/api/alarm'
import { ElMessage } from 'element-plus'
import { $bDialog } from '@/components/b-dialog'
import JcjkAlarmDetail from './jcjkAlarmDetail/index.vue'
import { useAuthStore } from '@/store/auth'
import dayjs from 'dayjs'
import { useDialog } from '@/hooks/useDialog'
// 定义表格数据类型
interface AlarmData {
  alarmTypeName: string
  alarmContent: string
  alarmTime: string
  alarmReason: string
  keepTimeStr2: string
  [key: string]: any
}

// 定义搜索参数类型
interface SearchParams {
  pageNumber: number
  pageSize: number
  total: number
}

// 表格数据
const tableData = ref<AlarmData[]>([])
const searchParams = ref<SearchParams>({
  pageNumber: 1,
  pageSize: 20,
  total: 0
})

// 加载状态
const loading = ref(false)

// 定时器ID
const interval = ref<number | null>(null)

// 获取未读告警列表
async function getUnreadAlarmListFun() {
  try {
    loading.value = true
    const { data } = await getUnreadAlarmList(searchParams.value)
    tableData.value = data.records
    searchParams.value.total = data.total
    searchParams.value.pageNumber = data.current
  } catch (error) {
    console.error('获取告警列表失败:', error)
    ElMessage.error('获取告警列表失败')
  } finally {
    loading.value = false
  }
}

// 处理页码变化
function handleCurrentChange(pageNumber: number) {
  searchParams.value.pageNumber = pageNumber
  getUnreadAlarmListFun()
}

// 组件挂载时
onMounted(() => {
  getUnreadAlarmListFun()
  // 每分钟刷新一次数据
  interval.value = window.setInterval(getUnreadAlarmListFun, 60000)
})

// 组件卸载时
onUnmounted(() => {
  if (interval.value) {
    clearInterval(interval.value)
    interval.value = null
  }
})

const authStore = useAuthStore()
const { openDialog } = useDialog()
function goBackTrack(row) {
  $bDialog.closeAll()
  authStore.setGlobalParamsItem('startTime', dayjs(row.beginTime).subtract(0.5, 'hour').format('YYYY-MM-DD HH:mm:ss'))
  authStore.setGlobalParamsItem('endTime', dayjs(row.beginTime).add(0.5, 'hour').format('YYYY-MM-DD HH:mm:ss'))
  authStore.setGlobalParamsItem('alarmId', row.alarmId)
  authStore.setGlobalParamsItem('cdCode', row.cdCode)
  authStore.setGlobalParamsItem('areaId', row.areaId)
  authStore.setGlobalParamsItem('areaName', row.areaName)
  authStore.setModeParamsItem('model', 'backtracking')

  // openDialog(row.areaName, BBackTrack, row, {
  //   width: 800,
  //   height: 430
  // })
}

function handleRowClick(row) {
  console.log(row)
  let comp = null
  if (row.alarmSource === '0') {
    // 矿数卫士
    comp = '恒值异常'
  } else if (row.alarmSource === '1') {
    // 监测监控
    comp = JcjkAlarmDetail
    row.id = row.alarmId
  } else if (row.alarmSource === '2') {
    comp = '风速异常'
  } else if (row.alarmSource === '3') {
    comp = '温度异常'
  }

  $bDialog({
    title: `${row.areaName} · ${row.alarmTypeName}`,
    Comp: comp,
    CompParams: row,
    width: 800,
    height: 430
  })
}
</script>

<style lang="scss" scoped>
.alarm-table {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.disposition-situation {
  display: flex;
  flex-direction: column;
  gap: 4px;
  align-items: center;
  justify-content: center;
}

.pagination {
  margin-top: 10px;
  display: flex;
  justify-content: flex-end;
}
</style>
